﻿using PubSubGeneric.WCFContracts;
using PubSubGeneric.WCFSubscriptionClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Serialize.Linq.Extensions;
using PubSubGeneric.WCFContracts.Utils;
using Serialize.Linq.Nodes;
using WCFDemoCommon;
using PubSubGeneric;
using PubSubGeneric.WCFPublishingClient;
namespace WCFDemoHost
{

    public partial class Form1 : Form, ISubscriber<Person>
    {
        public Form1()
        {
            InitializeComponent();
        }

        WCFSubscriptionService srv;



        private void Form1_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            srv = new WCFSubscriptionService();
            srv.Subscribe<Person>("Exit", this, (p => p.Age == 11));
            srv.Subscribe<Person>("Test", this);
        }

        void Test()
        {
            int[] ages = new int[] { 12, 45, 35, 76 };

            Expression<Func<Person, bool>> exp = (p => ages.Contains(p.Age));
            Expression<Func<Person, bool>> exp2 = (p => p.Age < 10);
            var body = Expression.AndAlso(exp.Body, exp2.Body);
            Expression<Func<Person, bool>> exp4 = Expression.Lambda<Func<Person, bool>>(body, exp.Parameters[0]);
            // WCFSerializer.Serialize(exp.ToExpressionNode());
            //    ExpressionNode en ;
            //srv.UnSubscribe<Person>("fgfg",this, exp);
        }


        public void OnTopicPublished(string topicName, Person args)
        {
            if (richTextBox1.InvokeRequired)
            {
                richTextBox1.Invoke(new Action<object>((arg) =>
                {
                    richTextBox1.AppendText(arg.ToString());

                }), topicName + ":" + args.ToString()+"\r\n");
            }
            else
            {
                richTextBox1.AppendText(topicName + ":" + args.ToString() + "\r\n");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            srv.UnSubscribe<Person>("Exit", this, (p => p.Age == 11));
            srv.UnSubscribe<Person>("Test", this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WCFPublishingService pub = new WCFPublishingService();
            pub.Publish<Person>("Exit", new Person() { Name = "WCF Bashir", Age = 11 });
            pub.Publish<Person>("Exit", new Person() { Name = "WCF Bashir", Age = 12 });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PublishingService pub = new PublishingService();
            pub.Publish<Person>("Exit", new Person() { Name = "Local Bashir", Age = 11 });
            pub.Publish<Person>("Exit", new Person() { Name = "Local Bashir", Age = 12 });
        }

    }

}
